<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <base href="/">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<table class="layui-table" lay-data="{url:'administrator/list', page:true, id:'datalist',toolbar:'#toptools'}" lay-filter="datalist">
    <thead>
    <tr>
        <th lay-data="{field:'id'}">ID</th>
        <th lay-data="{field:'account'}">账户</th>
        <th lay-data="{field:'name'}">账户名</th>
        <th lay-data="{field:'password'}">密码</th>
        <th lay-data="{field:'level'}">等级</th>
        <th lay-data="{field:'status'}">状态</th>
        <th lay-data="{toolbar:'#linetools'}">操作</th>
    </tr>
    </thead>
</table>

<!--行内工具-->
<script id="linetools" type="text/html">
    <button type="button" class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
    <button type="button" class="layui-btn layui-btn-xs" lay-event="delete">删除</button>
</script>
<!--顶部工具-->
<script id="toptools" type="text/html">
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <input type="text" name="text" placeholder="请输入查询内容" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" lay-event="check">查询</button>
            <button type="button" class="layui-btn" lay-event="add">新增</button>
        </div>
    </div>
</script>
<!--新增数据表单-->
<div style="display:none" id="addWin" lay-filter="addWin" class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">账户</label>
        <div class="layui-input-inline">
            <input type="text" name="account" required lay-verify="required" placeholder="请输入账户" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">账户名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" required lay-verify="required" placeholder="请输入账户名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="text" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">等级</label>
        <div class="layui-input-inline">
            <input type="text" name="level" required lay-verify="required" placeholder="请输入等级" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <input type="text" name="status" required lay-verify="required" placeholder="请输入状态" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <button type="button" lay-submit lay-filter="addsave" class="layui-btn">保存</button>
        <button type="button" class="layui-btn" id="addcancel">取消</button>
    </div>
</div>
<!--编辑数据表单-->
<div style="display:none" id="editWin" lay-filter="editWin" class="layui-form">
    <input name="id" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">账户</label>
        <div class="layui-input-inline">
            <input type="text" name="account" required lay-verify="required" placeholder="请输入账户" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">账户名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" required lay-verify="required" placeholder="请输入账户名" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">等级</label>
        <div class="layui-input-inline">
            <input type="text" name="level" required lay-verify="required" placeholder="请输入等级" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <input type="text" name="status" required lay-verify="required" placeholder="请输入状态" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <button type="button" lay-submit lay-filter="editsave" class="layui-btn">保存</button>
        <button type="button" class="layui-btn" id="editcancel">取消</button>
    </div>
</div>

<script src="layui/layui.all.js"></script>


<script>
    const $=layui.$;
    const table=layui.table;
    const form=layui.form;
    const layer=layui.layer;
    const upload=layui.upload;

    table.on("tool(datalist)",function (obj){
        let event=obj.event;
        //被点击按钮所在行的数据
        let data=obj.data;
        if(event=='delete'){
            layer.confirm("确认要删除吗?",function(){
                $.ajax({
                    url:'administrator/delete',
                    data:{id:data.id},
                    success:function(){
                        layer.msg("删除成功",{time:1800,icon:1});
                        table.reload("datalist",{page:{curr:1}});
                    }
                });
            });
        }else if(event=='edit'){

            //回现表单数据
            form.val("editWin",data);

            layer.open({
                type:1,
                title:'编辑',
                area:['400px','400px'],
                content:$("#editWin")
            });
        }
    });
    table.on("toolbar(datalist)",function (obj){
        let event=obj.event;
        if(event=='add'){
            layer.open({
                type:1,
                title:'新增',
                area:['400px','400px'],
                content:$("#addWin")
            });
        }else if(event=='check'){
            //获取输入框中的数据
            let text=$("[name='text']").val();
            //发送到后端 layui表格加载
            table.reload("datalist",{where:{'text':text},page:{curr:1}});
            $("[name='text']").val(text);
        }
    });
    //新增保存提交按钮事件
    form.on("submit(addsave)",function(data){
        console.log("--表单提交,并验证通过--");
        console.log(data);
        //发送ajax提交数据
        $.ajax({
            url:'administrator/add',
            data:data.field,
            success:function(result){
                //关闭窗口
                layer.closeAll();
                //提示信息
                layer.msg("添加成功",{icon:1,time:1800});
                //刷新表格
                table.reload("datalist");
            }
        });
        return false;//ajax已经提交了,禁止表单重复提交
    });
    //编辑保存提交按钮事件
    form.on("submit(editsave)",function(data){
        //发送ajax提交数据
        $.ajax({
            url:'administrator/edit',
            data:data.field,
            success:function(result){
                //关闭窗口
                layer.closeAll();
                //提示信息
                layer.msg("编辑成功",{icon:1,time:1800});
                //刷新表格
                table.reload("datalist");
            }
        });
        return false;//ajax已经提交了,禁止表单重复提交
    });
</script>
</body>
</html>